package com.charles.seckillPlatform.config;

import com.charles.seckillPlatform.schedule.DailyJob;
import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Quartz 配置
 */
@Configuration
public class QuartzConfig {
    
    @Bean
    public JobDetail dailyJobDetail() {
        return JobBuilder.newJob(DailyJob.class)
                .withIdentity("dailyJob")
                .storeDurably()
                .build();
    }

    @Bean
    public Trigger dailyJobTrigger() {
        // 因为每 24小时触发可能会和产品抢购撞时间，因此调整成凌晨12点。
        String cronExpression = "0 0 0 * * ?";
        return TriggerBuilder.newTrigger()
                .forJob(dailyJobDetail())
                .withIdentity("dailyJobTrigger")
                .withSchedule(CronScheduleBuilder.cronSchedule(cronExpression))
                .build();
    }
}
